package org.jeecg.modules.xxgx.cxtj.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.xxgx.cxtj.mapper.CsXxFptjDwdMapper;
import org.jeecg.modules.xxgx.cxtj.param.XxFptjDwdParam;
import org.jeecg.modules.xxgx.cxtj.service.ICsXxFptjDwdService;
import org.jeecg.modules.xxgx.cxtj.vo.XxFptjDwdDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CsXxFptjDwdServiceImpl implements ICsXxFptjDwdService {
    @Autowired
    private CsXxFptjDwdMapper csXxFptjDwdMapper;
    @Lazy
    @Resource
    private CommonAPI commonApi;

    @Override
    public List<XxFptjDwdDTO> getXxFpxxtjDwd(XxFptjDwdParam param) {
        List<XxFptjDwdDTO> list = csXxFptjDwdMapper.getXxFpxxtjDwd(param);
        //翻译字典
        //1、取字典
        List<DictModel> dictList = null;
        if("fplxdm".equals(param.getTjfs())){
            dictList = getDictList("xxfplxdm");
        } else if("sjlx".equals(param.getTjfs())){
            dictList = getDictList("cs_xx_sjlx","cname","ccode");
        } else if("djlx".equals(param.getTjfs())){
            dictList = getDictList("cs_xx_djlx","cname","ccode");
        } else if("sjyt".equals(param.getTjfs())){
            dictList = getDictList("cs_xx_ssyt","cname","ccode");
        } else if("sjly".equals(param.getTjfs())){
            dictList = getDictList("cs_xx_sjly","cname","ccode");
        } else if("create_by".equals(param.getTjfs())){
            dictList = getDictList("sys_user","realname","username");
        }

        if(dictList != null) {
            for (XxFptjDwdDTO xxFptjDwdDTO : list) {
                String value = xxFptjDwdDTO.getTjfs();
                String textValue = this.translDictText(dictList, value);
                xxFptjDwdDTO.setTjfs_dictText(textValue);
            }
        }

        return list;
    }

    /**
     * 取字典
     * @param dictCode
     * @return
     */
    private List<DictModel> getDictList(String dictCode) {
        List<DictModel> dictList = commonApi.queryDictItemsByCode(dictCode);
        return dictList;
    }
    private List<DictModel> getDictList(String dictTable ,String dicText, String dictCode) {
        dicText = oConvertUtils.getString(dicText, dictCode);
        List<DictModel> dictList = commonApi.queryTableDictItemsByCode(dictTable, dicText, dictCode);
        return dictList;
    }

    private String translDictText(List<DictModel> dictModels, String values) {
        if(StringUtils.isEmpty(values)){
            return "";
        }
        List<String> result = new ArrayList<>();

        // 允许多个逗号分隔，允许传数组对象
        String[] splitVal = values.split(",");
        for (String val : splitVal) {
            String dictText = val;
            for (DictModel dict : dictModels) {
                if (val.equals(dict.getValue())) {
                    dictText = dict.getText();
                    break;
                }
            }
            result.add(dictText);
        }
        return String.join(",", result);
    }
}
